reason for code like this: "do { something } while (0)"

reason for code like this: "do { something } while (0)"

am 15.05.2006 11:48:41 von Rajat Jain

Hi,

I'm asking a pretty amateur question.

I've come accross code like this huge number of times in the kernel:

#define __set_task_state(tsk, state_value) \
do { (tsk)->state = (state_value); } while (0)

What is the reason to use do while loop if it is only meant to be
executed just once? Why not a simple:

#define __set_task_state(tsk, state_value) \
{ (tsk)->state = (state_value); }

TIA,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: reason for code like this: "do { something } while (0)"

am 15.05.2006 12:07:58 von Adil Mujeeb

------=_Part_10341_33082120.1147687678296
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On 5/15/06, Rajat Jain wrote:
>
> Hi,
>
> I'm asking a pretty amateur question.
>
> I've come accross code like this huge number of times in the kernel:
>
> #define __set_task_state(tsk, state_value) \
> do { (tsk)->state =3D (state_value); } while (0)
>
> What is the reason to use do while loop if it is only meant to be
> executed just once? Why not a simple:
>
> #define __set_task_state(tsk, state_value) \
> { (tsk)->state =3D (state_value); }
>
> TIA,
>
> Rajat
>
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive: http://mail.nl.linux.org/kernelnewbies/
> FAQ: http://kernelnewbies.org/faq/
>
>
See this
http://kernelnewbies.org/FAQ/DoWhile0

Rgds,
Adil

------=_Part_10341_33082120.1147687678296
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline




On 5/15/06, =
Rajat Jain
<rajat.noi=
da.india@gmail.com
> wrote:

px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi,

I'm asking a pretty a=
mateur question.

I've come accross code like this huge number of tim=
es in the kernel:


#define __set_task_state(tsk, state_value)    &=
nbsp;         \
  =
;     do { (tsk)->state =3D (state_value); } while (=
0)

What is the reason to use do while loop if it is only meant to be=

executed just once? Why not a simple:


#define __set_task_state(tsk, state_value)    &=
nbsp;         \
  =
;     { (tsk)->state =3D (state_value); }

TIA=
,

Rajat

--
Kernelnewbies: Help each other learn about the =
Linux kernel.
Archive:      =20
http://mail.nl.linux.or=
g/kernelnewbies/

FAQ:        =
;   http://kernelnewbie=
s.org/faq/




See this

http://kernelnewbies=
..org/FAQ/DoWhile0

 

Rgds,

Adil


------=_Part_10341_33082120.1147687678296--

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/

Re: reason for code like this: "do { something } while (0)"

am 15.05.2006 12:11:14 von Anupam Kapoor

> What is the reason to use do while loop if it is only meant to be
> executed just once? Why not a simple:

look at kernelnewbies faq/google/search the archives for this list.

anupam

--
In the beginning was the lambda, and the lambda was with Emacs, and
Emacs was the lambda.
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: reason for code like this: "do { something } while (0)"

am 15.05.2006 12:12:13 von Jesper Juhl

On 15/05/06, Rajat Jain wrote:
> Hi,
>
> I'm asking a pretty amateur question.
>
> I've come accross code like this huge number of times in the kernel:
>
> #define __set_task_state(tsk, state_value) \
> do { (tsk)->state = (state_value); } while (0)
>
> What is the reason to use do while loop if it is only meant to be
> executed just once? Why not a simple:
>
> #define __set_task_state(tsk, state_value) \
> { (tsk)->state = (state_value); }
>

http://www.kernelnewbies.org/FAQ/DoWhile0

--
Jesper Juhl
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs